import React, {Component} from 'react'
import PropTypes from 'prop-types'
import {TouchableOpacity,AsyncStorage} from 'react-native'
import FontAwesome from 'react-native-vector-icons/FontAwesome'

export default class BaseItem extends Component {
    static propTypes = {
        projectModel: PropTypes.object,
        onFavorite: PropTypes.func,
        onSelect: PropTypes.func,
    }

    constructor(props) {
        super(props)
        this.state = {
            isFavorite: this.props.projectModel.isFavorite,
        }
    }

    static getDerivedStateFromProps(nextProps, prevState) {
        const {isFavorite} = nextProps.projectModel
        if (isFavorite !== prevState.isFavorite) {
            return {
                isFavorite
            }
        }
        return null
    }

    setFavoriteState(isFavorite) {
        this.props.projectModel.isFavorite = isFavorite
        this.setState({
            isFavorite,
        })
    }

    onItemClick() {
        this.props.onSelect(isFavorite => this.setFavoriteState(isFavorite))


    }

    onPressFavorite() {
        this.setFavoriteState(!this.state.isFavorite)
        this.props.onFavorite(this.props.projectModel.item, !this.state.isFavorite)
    }

    _favoriteIcon() {
        // const {theme} = this.props
        return <TouchableOpacity
            style={{padding: 6}}
            onPress={() => {
                this.onPressFavorite()
            }}
            underlayColor={'transparent'}
        >
            <FontAwesome
                name={this.state.isFavorite ? 'star' : 'star-o'}
                size={16}
                style={{color: '#678'}}
            />
        </TouchableOpacity>
    }
}